const express = require('express');
const app = express();
const router = express.Router();
const db = require('../../db'); // 引入数据库封装模块
const $sql = require('../../sqlMap');
router.get('/menu', function(req, res) {
    db.query($sql.menu.SELECT_S, function(err, result) {
        let child = [];
        // console.log('child3',req.session.userName,req.session.cookie.maxAge)
        let menu = [];
        if (result) {
            for (let i = 0; i < result.length; i++) {
                if (result[i].pid === 0) {
                    result[i].child = [];
                    menu.push(result[i]);
                } else {
                    child.push(result[i]);
                }
            }
            for (let i = 0; i < menu.length; i++) {
                if (child.length > 0 && menu[i]) {
                    for (item in child) {
                        if (child[item].pid == menu[i].id) {
                            menu[i].child.push(child[item]);
                        }
                    }
                }
            }
            // db.jsonWrite(res, { code: 0, data: menu, msg: '请求成功' });
            res.json({ code: 0, data: menu, msg: '请求成功' })
        }
    });
});
module.exports = router;